﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Controller;

namespace QLKS
{
    public partial class FrmInfoEmployee : Form
    {
        Controller_Employee cont_Employ = new Controller_Employee();
        DataSet ds = new DataSet();
        public FrmInfoEmployee()
        {
            InitializeComponent();
            send = new SendInfo(getInfo);
        }

        public delegate void SendInfo();
        public SendInfo send;
        private void getInfo()
        {
            Load_List_All();
        }


        public void Load_List_All()
        {
            ds = cont_Employ.SelectAll_Employee();
            dgvListEmploy.DataSource = ds.Tables[0];
        }
        private void FrmInfoEmployee_Load(object sender, EventArgs e)
        {
            Load_List_All();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            string search = txtSearchString.Text.ToLower();
            ds = cont_Employ.FindbyName(search);
            dgvListEmploy.DataSource = ds.Tables[0];
            
        }

        private void txtSearchString_TextChanged(object sender, EventArgs e)
        {
            btnFind_Click(sender, e);
        }

        private void tbtnAddEmploy_Click(object sender, EventArgs e)
        {
            foreach (Form f in MdiChildren)
            {
                if (f is FrmAddEmployee)
                {
                    f.Activate();
                    return;
                }
            }
            FrmAddEmployee frmAdd = new FrmAddEmployee();
            frmAdd.MdiParent = ParentForm as Frm_Main;
            frmAdd.Show();
        }

        private void dgvListEmploy_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int r = dgvListEmploy.CurrentRow.Index;
            string manv = dgvListEmploy.Rows[r].Cells["MaNhanVien"].Value.ToString();
            foreach (Form f in MdiChildren)
            {
                if (f is FrmAddEmployee)
                {
                    this.ActivateMdiChild(f);
                    //f.Activate();
                    return;
                }
            }
            FrmAddEmployee obj = new FrmAddEmployee();
            obj.sender(manv);
            obj.MdiParent = this.MdiParent; //(Frm_Main)MdiParent;
            obj.Show();


        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            int r = dgvListEmploy.CurrentRow.Index;
            string manv = dgvListEmploy.Rows[r].Cells[0].Value.ToString();
            DialogResult dl = MessageBox.Show("Bạn thực sự muốn xóa Nhân viên này","Thông báo",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
            if (dl == DialogResult.OK)
                if (cont_Employ.Delete_Employee(manv))
                {
                    MessageBox.Show("Xóa thành công", "Thông báo");
                    Load_List_All();
                }
                else
                    MessageBox.Show("Không thể xóa!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {

        }


    }
}
